package com.gxa.saas.controller;


import com.gxa.saas.pojo.BrandFirst;
import com.gxa.saas.pojo.BrandSecond;
import com.gxa.saas.service.BrandService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bind.annotation.Default;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author CZJ
 * @date 2022/6/7 11:15
 */
@Api(value = "一级商品分类相关接口")
@RestController
@RequestMapping("/brandFirst")
@CrossOrigin
public class BrandFirstController {

    @Autowired
    BrandService brandService;


    private JsonResult jsonResult=new JsonResult();

    @ApiOperation(value = "获取一级商品列表",produces = "query")
    @PostMapping("/select")
    /**
     * 获取一级商品列表
     */
    public Map getBrand(){
        List<BrandFirst> brandFirsts = brandService.selectAll();
        jsonResult.setData(brandFirsts);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "分页查询一级商品接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询页数",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页查询数",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @PostMapping("/selectByPage")
    /**
     * 分页查询一级品牌信息
     */
    public Map getBrandByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size){
        List<Map<String, Object>> maps = brandService.selectByPage(page, size);
        List<BrandFirst> brandFirsts = brandService.selectAll();
        jsonResult.set("number",brandFirsts.size());
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(maps);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "添加一级品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandFirst",value = "添加的对象",dataType = "BrandFirst",paramType = "body")

    })
    @PostMapping("/addBrandF")
    /**
     * 添加品牌接口
     */
    public Map addBrandFirst(@RequestBody BrandFirst brandFirst){
        brandFirst.setLevel("一级");
        System.out.println(brandFirst);
        Integer insert = brandService.insert(brandFirst);
        System.out.println(brandFirst);
        jsonResult.setData(insert);
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "查询的条件",dataType = "String",paramType = "query")

    })
    @PostMapping("/selectLike")
    /**
     * 模糊查询
     */
    public Map selectLike(String name){
        List<BrandFirst> brandFirsts = brandService.selectLike(name);
        jsonResult.setData(brandFirsts);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据id修改品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brand",value = "修改的对象",dataType = "Brand",paramType = "body")
    })
    @PostMapping("/updateBrand")
    /**
     * 修改品牌接口
     */
    public Map updateBrand(@RequestBody BrandFirst brand){
        Integer update = brandService.update(brand);
        jsonResult.setCode("200");
        jsonResult.setData(update);
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据id删除品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除的id",dataType = "Integer",paramType = "query")

    })
    @PostMapping("/deleteBrand")
    /**
     * 根据id删除
     */
    public Map deleteBrand(Integer id){
        Integer delete = brandService.delete(id);
        jsonResult.setMsg("删除成功");
        jsonResult.setData(delete);
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "查询全部一级品牌")
    @PostMapping("/selectAll")
    /**
     * 查询全部一级品牌的名称
     * @return
     */
    public Map selectAll(){
        List<BrandFirst> brandFirsts = brandService.selectAll();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(brandFirsts);
        return jsonResult.getMap();
    }

}
